package com.ufox.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ufox.entity.*;
import com.ufox.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 会员信息表 前端控制器
 * </p>
 *
 * @author 优狐科技-www.hbufox.com
 * @since 2022-05-31
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private IMemberService memberService;

    @GetMapping("/{id}")
    protected Result selectById(@PathVariable Integer id) {
        Member member = memberService.selectMemberById(id);
        return Result.ok(member);
    }


    //分页 条件查询
    @PostMapping("/{page}/{size}")
    public Result findAllCond(@RequestBody MemberCond memberCond, @PathVariable int page, @PathVariable int size) {
        Page<Member> pageInfo = new Page<>(page, size);
        Page<Member> list = memberService.selectAllByCond(pageInfo, memberCond);
        return Result.ok(list);
    }

    //添加  或 修改
    @PostMapping
    public Result saveOrUpdateBook(@RequestBody Member member) {
        Integer integral = member.getIntegral();
        BigDecimal money = member.getMoney();
        if (integral < 0 || money.compareTo(BigDecimal.ZERO) <  0) {
            return Result.error("开卡金额或可用积分不能为负数！");
        } else {
            boolean b = memberService.saveOrUpdate(member);
            return  (b ? Result.ok() : Result.error(""));
        }
    }

    //删除
    @DeleteMapping("/{id}")
    public Result delMember(@PathVariable Integer id) {
        boolean b = memberService.removeById(id);
        return  (b ? Result.ok() : Result.error(""));
    }

    //批量删除

    @PostMapping("/batch")
    public Result delMembersBatch(@RequestBody List<Integer> ids) {
        boolean b = memberService.removeByIds(ids);
        return  (b ? Result.ok() : Result.error(""));
    }

    @GetMapping("/findByCardNum/{cardNum}")
    protected Result findByCardNum(@PathVariable String cardNum) {
        LambdaQueryWrapper<Member> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(cardNum), Member::getCardNum, cardNum);
        Member member = memberService.getOne(wrapper);
        Result result = null;
        if (member != null) {
            result = Result.error("该卡号已存在！");

        }
        return result;
    }

    @GetMapping("/findByPhone/{phone}")
    protected Result findByPhone(@PathVariable String phone) {
        LambdaQueryWrapper<Member> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(phone), Member::getPhone, phone);
        Member member = memberService.getOne(wrapper);
        Result result = null;
        if (member != null) {
            result = Result.error("该手机号已存在！");
        }
        return result;
    }


}

